/**
 ******************************************************************************
 * @file desktop.c
 * @author GX (2524913364@qq.com)
 * @brief 
 * @version 1.0.0 
 * @date 2022-07-11
 * 
 * @copyright Copyright (c) 2022
 * 
 ******************************************************************************
 *----------------------------------------------------------------------------*
 *  Remark         : Description                                              *
 *----------------------------------------------------------------------------*
 *  Change History :                                                          *
 *  <Date>     | <Version> | <Author>       | <Description>                   *
 *----------------------------------------------------------------------------*
 *  2022-07-11 | 1.0.0     | GX             | Create file                     *
 *----------------------------------------------------------------------------*
 *                                                                            *
 ******************************************************************************
 */


/*----------------------------- start of file -------------------------------*/
#include "app_desktop.h"
#include "lcd.h"
#include "touch_screen.h"


static INTERFACE_CHOOSE desktop_GetKey(int x, int y);


/**
 * @brief 
 * 
 * @return true 
 * @return false 
 */
INTERFACE_CHOOSE desktop_interface(void)
{
  printf("in to desktop\n");
  /*------------------------------开启显示屏幕-----------------------------*/
  int fd_lcd;                             /* 打开文件获取文件描述 */
  int *fb_base = NULL;                    /* 映射后显存的地址 */
  struct fb_var_screeninfo vinfo;         /* 显卡设备的可变属性结构体 */
	/* 打开文件 */
	fd_lcd = open(LCD_PATH, O_RDWR);
	if(fd_lcd == -1)
	{
		perror("open lcd");
    return INTERFACE_UNLOCK;
	}
  else
	{
		/* 显存映射 */
		fb_base = mmap(NULL, LCD_BUFF_SIZE*LCD_BLOCK,  PROT_READ|PROT_WRITE,
					MAP_SHARED, fd_lcd, 0);
		if(fb_base == MAP_FAILED) 
		{
			perror("申请显存失败");
      close(fd_lcd);
      return INTERFACE_UNLOCK;
		} 

    if(ioctl(fd_lcd, FBIOGET_VSCREENINFO, &vinfo) != 0)/* 获取可变属性 */
    {
        perror("获取LCD设备可变属性信息失败");
        return INTERFACE_UNLOCK;
    }
  }

  printf("start to desktop\n");
  TOUCH *tc = malloc(sizeof(TOUCH));/* 存储触摸屏信息 */
  Renew_tcState();                  /* 更新状态机 */

  INTERFACE_CHOOSE itface = INTERFACE_NONE;  /* 用来返回的界面值 */ 

  lcd_show_bmp(LCD_DISPLAY(fb_base, ++view_block), DESKTOP_WALLPAPER_PATH, 0,  0);
  LCD_SET_DISPLAY(fd_lcd, &vinfo, view_block); 


  while (1)
  {
    get_tcState(tc);
    if( tc->response )
    {
      // printf("respongse\n");
      continue;
    }

    switch (tc->state)
    {
    case STATE_SINGLE_CLICK:  /* 单击 */
      response_over();
      itface = desktop_GetKey(tc->x_last, tc->y_last);
      printf("itface = %d\n", itface);
      break;

    default:
      response_over();
      break;

    }

    if(( itface != INTERFACE_NONE ) && ( itface !=  INTERFACE_DESKTOP ))
    {
      printf("out: face %d\n", itface);
      break;
    }

  }




  view_block = 0;
  free(tc);
  munmap(fb_base, LCD_BUFF_SIZE*LCD_BLOCK);
  close(fd_lcd);

  printf("out desktop\n");
  return itface;
}


/**
 * @brief 获取按键键值
 * 
 * @param x 输入坐标
 * @param y 输入坐标
 * @return UNLOCK_KEY 返回键值
 */
static INTERFACE_CHOOSE desktop_GetKey(int x, int y)
{
  INTERFACE_CHOOSE itface;

  /* 第一行应用 */
  if(( y >= DESKTOP_LINE1_Y_START) && ( y <=  DESKTOP_LINE1_Y_END))
  {
    /* 第一列 */
    if(( x >=  DESKTOP_ROW1_X_START) && ( x <= DESKTOP_ROW1_X_END ))
    {
      printf("pokemon\n");
      itface = INTERFACE_POKEMON;
    }

    /* 第二列 */
    else if(( x >=  DESKTOP_ROW2_X_START) && ( x <= DESKTOP_ROW2_X_END ))
    {
      printf("photo\n");
      itface = INTERFACE_PHOTO;
    }

    /* 第三列 */
    else if(( x >=  DESKTOP_ROW3_X_START) && ( x <= DESKTOP_ROW3_X_END ))
    {
      printf("artmaster\n");
      itface = INTERFACE_DRAW;
    }
  }
  else if(( x >= DESKTOP_BK_X_START ) && ( x <= DESKTOP_BK_X_END ) &&
          ( y >= DESKTOP_BK_Y_START ) && ( y <= DESKTOP_BK_Y_END ))
  {
    printf("go back\n");
    itface = INTERFACE_UNLOCK;
  }
  else
  {
    printf("none\n");
    itface = INTERFACE_NONE;
  }

  return itface;
}

 

/*------------------------------ end of file --------------------------------*/

